package com.qsw.refreshrecyclerview.RefreshRecyclerView.view;

import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * RecyclerView线性布局边距
 */
public class LinearSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int mJumpCount;// 跳过数量（不设置边距）
    private int mItemType; // 要设置边距的条目类型

    /**
     * 间距padding
     */
    private int mSpacing;

    /**
     * 间距padding
     */
    private int mLeft;
    private int mTop;
    private int mRight;
    private int mBottom;

    /**
     * true：同样的边距；false：不同边距
     */
    private boolean isSameSpacing;

    /**
     * RecyclerView线性布局边距
     *
     * @param spacings 间距padding
     */
    public LinearSpacingItemDecoration(int spacings) {
        this(0, 0, spacings);
    }

    /**
     * RecyclerView线性布局边距
     *
     * @param jumpCount 跳过的数量
     * @param itemType  要操作边距的类型
     * @param spacings  间距padding
     */
    public LinearSpacingItemDecoration(int jumpCount, int itemType, int spacings) {
        this.mJumpCount = jumpCount;
        this.mItemType = itemType;
        mSpacing = spacings;
        isSameSpacing = true;
    }

    /**
     * RecyclerView线性布局边距
     *
     * @param left   左边距
     * @param top    上边距
     * @param right  右边距
     * @param bottom 下边距
     */
    public LinearSpacingItemDecoration(int left, int top, int right, int bottom) {
        this(0, 0, left, top, right, bottom);
    }

    /**
     * RecyclerView线性布局边距
     *
     * @param jumpCount 跳过的数量
     * @param itemType  要操作边距的类型
     * @param left      左边距
     * @param top       上边距
     * @param right     右边距
     * @param bottom    下边距
     */
    public LinearSpacingItemDecoration(int jumpCount, int itemType, int left, int top, int right, int bottom) {
        this.mJumpCount = jumpCount;
        this.mItemType = itemType;
        this.mLeft = left;
        this.mTop = top;
        this.mRight = right;
        this.mBottom = bottom;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view) - mJumpCount;
        if (position >= 0 && parent.getChildViewHolder(view).getItemViewType() == mItemType) {
            if (isSameSpacing) {
                outRect.left = mSpacing;
                outRect.top = mSpacing;
                outRect.right = mSpacing;
                outRect.bottom = mSpacing;
            } else {
                outRect.left = mLeft;
                outRect.top = mTop;
                outRect.right = mRight;
                outRect.bottom = mBottom;
            }
        }
    }
}
